send and receive in socket [closed]

Posted by user3696492 on Stack Overflow See other posts from Stack Overflow or by user3696492
Published on 2014-06-01T09:58:17Z Indexed on 2014/06/08 21:25 UTC
Read the original article Hit count: 170

Filed under:
|
|

I have trouble in sending an object through socket in c#, my client can send to server but server can't send to client, i think there is something wrong with the client.

Server

    private void Form1_Load(object sender, EventArgs e)
    {
        CheckForIllegalCrossThreadCalls = false;
        Thread a = new Thread(connect);
        a.Start();
    }

    private void sendButton_Click(object sender, EventArgs e)
    {
        client.Send(SerializeData(ShapeList[ShapeList.Count - 1]));
    }

    void connect()
    {
        try
        {               
            server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5555);
            server.Bind(iep);
            server.Listen(10);
            client = server.Accept();
            while (true)
            {
                byte[] data = new byte[1024];
                client.Receive(data);
                PaintObject a = (PaintObject)DeserializeData(data);
                ShapeList.Add(a);
                Invalidate();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

client

private void Form1_Load(object sender, EventArgs e)
    {
        CheckForIllegalCrossThreadCalls = false;
        Thread a = new Thread(connect);
        a.Start();
    }

    private void SendButton_Click(object sender, EventArgs e)
    {
        client.Send(SerializeData(ShapeList[ShapeList.Count - 1]));
    }

    void connect()
    {
        try
        {            
            client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5555);
            client.Connect(iep);
            while (true)
            {
                byte[] data = new byte[1024];
                client.Receive(data);
                PaintObject a = (PaintObject)DeserializeData(data);
                ShapeList.Add(a);
                Invalidate();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

© Stack Overflow or respective owner

Related posts about c#

Related posts about sockets